﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Yi_demo.Entities;

namespace Yi_demo.Controllers
{
    [Route("api/student")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        [HttpGet]
        public string GetTest()
        {
            return "已经进来了 API 接口";
        }

        [HttpGet("get")]
        public ActionResult GetAstudents()
        {
            base.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");//跨域问题
            return new JsonResult(
                new StudentEntity() { Id = 2, Name = "小王同学", Password = "baidu" }
                );
        }

        [HttpGet("{studentid}")]
        public ActionResult GetStudentofId([FromRoute] int studentid)
        {
            var entities = new List<StudentEntity>()
            {
                new StudentEntity() { Id =1,Name = "小张同学"},
                new StudentEntity() { Id =2,Name = "小王同学" }
            };
            if (studentid > 2)
            {
                return NotFound();
            }
            return new JsonResult(entities[studentid - 1]);
        }
        [HttpGet("getid")]
        public ActionResult GetStudentfromId([FromQuery] int studentid)
        {
            var studentResulta = new StudentEntity()
            {
                Id = studentid,
                Name = "小陈同学",
                Password = "123"
            };
            return new JsonResult(studentResulta);
        }
        [HttpPost("post")]
        public ActionResult CreateStudent([FromBody] studentAdd studentAdd)
        {
            return new JsonResult(studentAdd);
        }
        [HttpDelete("delete")]
        public string DeletStudent(int id)
        {
            return $"Id是{id} 的同学已经被删除";
        }
    }
}
